# -*- coding: utf-8 -*-
# @Time : 2022/2/28 19:26
# @Author : Liuqing
# @File : 水门桥.py
# @Software : PyCharm
import codecs

import requests
import parsel
import csv

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/98.0.4758.102 Safari/537.36 "
}
for page in range(0, 11):
    url = f"https://movie.douban.com/subject/35613853/comments?start={page}&limit=20&status=P&sort=time"
    data_html = requests.get(url, headers=header).text
    selector = parsel.Selector(data_html)
    comment_list = selector.css(".comment-item")
    for comment in comment_list:
        user_name = comment.css(".comment-info a::text").get().strip()
        short = comment.css(".short::text").get().strip()
        time = comment.css(".comment-time::text").get().strip()
        vote_count = comment.css(".votes.vote-count::text").get().strip()
        # print(user_name, short)
        with open("长津湖之水门桥.csv", mode="a", encoding="utf-8", newline="") as f:
            csv_writer = csv.writer(f, codecs.BOM_UTF8)
            csv_writer.writerow([user_name, short, time, vote_count])
